package page.sample.userpackage;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.markup.repeater.data.ListDataProvider;

import page.sample.AuthorizationPage;
import page.sample.administratorpackage.ShowGoodsPage;
import dao.impl.GoodsDao;
import dao.impl.UserDao;
import entity.Administrator;
import entity.Goods;
import entity.User;

public class BuyPage extends WebPage {

	public BuyPage(final User user)
	{
			final GoodsDao goodsDao = new GoodsDao(
					app.MyFirstWebApplication.DATA_XML_FOLDER);
			final UserDao userDao = new UserDao(
					app.MyFirstWebApplication.DATA_XML_FOLDER);

			add(new DataView<Goods>("table-rows", new ListDataProvider<Goods>(
					goodsDao.getAll())) {
				@Override
				protected void populateItem(final Item<Goods> item) {
					final Goods goods = item.getModelObject();
					// add data for table cell in row
					item.add(new Label("name", goods.getName()));
					item.add(new Label("price", goods.getPrice()));
					
					item.add(new Link("addShoppingCart") {
						@Override
						public void onClick() {
							
							user.getShoppingCart().add(goods);
							userDao.update(user);
						}
						});
				}
			});
			
			add(new Link("buy") {

				@Override
				public void onClick() {
					
					setResponsePage(new BuyPage(user));
					
				}

			});

			add(new Link("showShoppingCart") {

				@Override
				public void onClick() {
					setResponsePage(new ShowShoppingCartPage(user));
				}

			});
			
			add(new Link("exit") {

				@Override
				public void onClick() {
					setResponsePage(new AuthorizationPage());

				}

			});
	}
}
